﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;

namespace LearningBubbles
{
    class InteractionGrab : Interaction
    {
        public InteractionGrab()
        {
        }

        void Interaction.Interact(Element e, Vector2 handPosition, Player[] players, int playerNum, int handNr)
        {
            int lowest = 9999;
            double distance = Math.Sqrt(Math.Pow((e.Position.X - (handPosition.X)), 2) + Math.Pow((e.Position.Y - handPosition.Y), 2));
            if (distance < (Textures.ElementTexture.Bounds.Width+Textures.ElementTexture.Bounds.Width)/2)
            {
                if ((handNr == 0 && players[playerNum].LHandHasElement == false)
                    ||(handNr == 1 && players[playerNum].RHandHasElement == false)
                    || e.GrabbedBy == playerNum)
                {
                    if (e.Speed != 0) e.PreviousSpeed = e.Speed;
                    e.Speed = 0;
                    e.Position = handPosition;
                    e.GrabbedBy = playerNum;
                }


                if (handNr == 0 && players[playerNum].LHandHasElement == false) players[playerNum].LHandHasElement = true;
                if (handNr == 1 && players[playerNum].RHandHasElement == false) players[playerNum].RHandHasElement = true;
                
            }
        }

        void Interaction.ReleaseElement(Element e, Player[] players, int playerNum, int handNr)
        {
            if (e.GrabbedBy == playerNum)
            {
                if (e.Speed == 0) e.Speed = e.PreviousSpeed;
                e.GrabbedBy = 5;
                if(handNr == 0) players[playerNum].LHandHasElement = false;
                else players[playerNum].RHandHasElement = false;
                players[playerNum].Hands[handNr].HandInteracts = false;
            }
        }
    }
}
